using AutoMapper;
using BackData;
using Domain;
using Domain.DTOs.ProcessSteps;
using Infratructure;
using MediatR;
using SqlSugar;
using Team.API.Applocation.Command.Makesteps;

namespace Team.API.Applocation.Hanldler.Makesteps
{
    /// <summary>
    /// 工艺工序日志查询处理器
    /// </summary>
    public class GetMakestepLogsHandler : IRequestHandler<GetMakestepLogsCommand, APIResult<List<MakestepLogDTO>>>
    {
        private readonly IRepository<Nlogmodel> repository;
        private readonly IMapper mapper;

        public GetMakestepLogsHandler(IRepository<Nlogmodel> repository, IMapper mapper)
        {
            this.repository = repository;
            this.mapper = mapper;
        }

        public async Task<APIResult<List<MakestepLogDTO>>> Handle(GetMakestepLogsCommand request, CancellationToken cancellationToken)
        {
            var list = await repository.GetAll()
                .Where(x => !x.IsDel && x.Otherkey == request.MakestepId && x.Category == request.Category)
                //.OrderByDescending(x => x.Createtime)
                .ToListAsync();

            var dtos = mapper.Map<List<MakestepLogDTO>>(list);
            return APIResult<List<MakestepLogDTO>>.Ok("查询成功", dtos);
        }
    }
}

